package com.hengxinguotong.zhihuichengjian.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;

import com.google.gson.Gson;
import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.adapter.MyExpandableListView;
import com.hengxinguotong.zhihuichengjian.bean.GroupInfoRes;
import com.hengxinguotong.zhihuichengjian.bean.GroupListRes;
import com.hengxinguotong.zhihuichengjian.utils.SPUtil;
import com.hengxinguotong.zhihuichengjian.utils.Utils;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;
import com.lidroid.xutils.http.RequestParams;

import org.apache.http.entity.StringEntity;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 班组信息维护
 * Created by Administrator on 2017/8/25.
 */

public class GroupInfoMaitainActivity extends BaseActivity implements View.OnClickListener{

    private GroupInfoMaitainActivity mActivity;

    @BindView(R.id.tv_title)
    HXTextView tvTitle;
    @BindView(R.id.add)
    HXTextView add;
    @BindView(R.id.expandableListView)
    ExpandableListView expandableListView;

    private MyExpandableListView adapter;
    List<GroupInfoRes.CompanyBean> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group_info);
        ButterKnife.bind(this);
        mActivity = this;
        tvTitle.setText("班组信息");
        expandableListView.setGroupIndicator(null);
        if (hasAdd()) {
            add.setVisibility(View.VISIBLE);
        }
        setListener();
    }

    @Override
    protected void onStart() {
        super.onStart();
        getGroupList("加载中…");
    }

    private void setListener(){
        expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                return false;//返回true，把他的组点击事件屏蔽，不许闭合分组
            }
        });
        expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                Intent intent = new Intent(mActivity, GroupActivity.class);
                intent.putExtra("authority", authority);
                intent.putExtra("teamId", list.get(groupPosition).getData().get(childPosition).getId());
                intent.putExtra("teamName", list.get(groupPosition).getData().get(childPosition).getName());
                startActivityForResult(intent, 1);
                return false;
            }
        });
    }

    private void getGroupList(String alert) {
        JSONObject jsonObject = new JSONObject();

        try {
            jsonObject.put("constructionId", SPUtil.getString(this, "constructionid"));
        } catch (JSONException e) {
            e.printStackTrace();
        }

        RequestParams params = new RequestParams();
        params.addHeader("Content-type", "application/json");
        try {
            params.setBodyEntity(new StringEntity(jsonObject.toString(), "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Utils.requestData(alert, this, "/team/queryTeamGroupByCompany/" + SPUtil.getString(this, "uuid"), params, new Utils.ListenerJson() {
            @Override
            public void onSuccess(String json) {
                if (!Utils.isEmpty(json)) {
                    Gson gson = new Gson();
                    GroupInfoRes groupInfoRes = gson.fromJson(json, GroupInfoRes.class);
                    list = groupInfoRes.getValue();
                    process(list);
                }
            }

            @Override
            public void onFailure(String json) {

            }
        });
    }

    private void process(List<GroupInfoRes.CompanyBean> datas){
        adapter = new MyExpandableListView(datas, mActivity);
        expandableListView.setAdapter(adapter);
        for(int i = 0; i < datas.size(); i++){
            expandableListView.expandGroup(i);//循环遍历展开所有分组
        }
    }

    @OnClick({R.id.tv_back, R.id.add})
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_back:
                finish();
                break;
            case R.id.add:
                Intent intent = new Intent(this, AddTeamActivity.class);
                startActivityForResult(intent, 1);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 1 && expandableListView != null) {
            getGroupList("加载中…");
        }
    }
}
